package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbStockBean;
import id.net.iconpln.bb.client.model.PageBbTrackingBean;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;

public class PageBbTracking extends LayoutContainer {
	Grid<PageBbTrackingBean> grid;
	private FormData formData;
	private FormBinding formBindings; 
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Info Tracking");  
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));
	  
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbStockBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbStockBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	     
	  
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true);  
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(grid, new RowData(.6, 1)); 
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Informasi Tracking");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2);
	    
	    final TextField<String> txtnama_pemasok = new TextField<String>();
	    txtnama_pemasok.setName("nama_pemasok");
	    txtnama_pemasok.setFieldLabel("Nama Pemasok");  
	    fieldSet2.add(txtnama_pemasok, formData);
	    
	    final TextField<String> txtnama_transport = new TextField<String>();
	    txtnama_transport.setName("nama_transport");
	    txtnama_transport.setFieldLabel("Nama Transport");  
	    fieldSet2.add(txtnama_transport, formData);
	    
	    final TextField<String> txtjenis_transport = new TextField<String>();
	    txtjenis_transport.setName("jenis_transport");
	    txtjenis_transport.setFieldLabel("Jenis Transport");  
	    fieldSet2.add(txtjenis_transport, formData);
	    
	    final TextField<String> txtnama_kit = new TextField<String>();
	    txtnama_kit.setName("speed");
	    txtnama_kit.setFieldLabel("Kecepatan");  
	    fieldSet2.add(txtnama_kit, formData);
	    
	    DateTimeFormat dtFormat = DateTimeFormat.getFormat("d/M/yy h:mma");
	    final DateField txtalamat = new DateField(); 
	    txtalamat.setName("waktu_catat");
	    txtalamat.setFieldLabel("Waktu Catat");
	    txtalamat.getPropertyEditor().setFormat(dtFormat);
	    fieldSet2.add(txtalamat, formData);  
	  
	    final TextField<String> txtkapasitas = new TextField<String>();
	    txtkapasitas.setName("latitude");
	    txtkapasitas.setFieldLabel("Latitude");  
	    fieldSet2.add(txtkapasitas, formData);  
	    
	    final TextField<String> txttype_kit = new TextField<String>();
	    txttype_kit.setName("longitude");
	    txttype_kit.setFieldLabel("Longitude");
	    fieldSet2.add(txttype_kit, formData); 
	    
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        txtkapasitas.setValue("");
	        txtalamat.setValue(null);
	        txtnama_kit.setValue("");
	        txttype_kit.setValue("");
	        
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();
	        MessageBox.alert("Message", "DATABASE sedang dalam Perbaikan", null);
	      }  
	    }));  

	    panel.add(p);
	    return panel;  
	  }  
	  
	  private Grid<PageBbTrackingBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer id = new RowNumberer();  
	    id.setWidth(30);  
	    
	    ColumnConfig nm_pemasok = new ColumnConfig("nama_pemasok", "Nama Pemasok", 200);
	    nm_pemasok.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig nm_transport = new ColumnConfig("nama_transport", "Nama Transport", 200);
	    nm_transport.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig jenis_transport = new ColumnConfig("jenis_transport", "Jenis Transport", 200);
	    jenis_transport.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig alamat = new ColumnConfig("speed", "Kecepatan", 100);
	    alamat.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig kapasitas = new ColumnConfig("waktu_catat", "Waktu Catat", 120);
	    kapasitas.setAlignment(HorizontalAlignment.RIGHT);
	    kapasitas.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
	    
	    ColumnConfig latitude = new ColumnConfig("latitude", "Latitude", 200);
	    latitude.setAlignment(HorizontalAlignment.RIGHT);
	    
	    ColumnConfig longitude = new ColumnConfig("longitude", "Longitude", 200);
	    longitude.setAlignment(HorizontalAlignment.RIGHT);
	    
	    columns.add(id);
	    columns.add(nm_pemasok);
	    columns.add(nm_transport);
	    columns.add(jenis_transport);
	    columns.add(alamat);
	    columns.add(kapasitas);
	    columns.add(latitude);
	    columns.add(longitude);
	   
	    ListStore<PageBbTrackingBean> store = new ListStore<PageBbTrackingBean>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getTracking());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbTrackingBean>(store, cm);
	    grid.setBorders(true);
	    
	    return grid;  
	  }  
 }
